package com.aquarium.ui;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Path;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.aquarium.R;
import com.aquarium.alarm.AlarmManager;
import com.aquarium.data.HistoryDataBase;
import com.aquarium.util.SensorData;

public class HistoryActivity extends Activity {
	Context mContext;
	ListView listView;
	LinearLayout layout;
	MyItemAdapter adapter;
	ImageButton backButton;
//	ImageButton historyButton;
	HistoryDataBase dataBase;
	private List<SensorData> mData;
	AlarmManager alarmManager;
	ArrayList<Path> mPaths;
	Integer[] mItemName;
	Integer[] mIconIDs;
	Integer[] mColors;
	int tmpx = 30;
	int tmpy = 150;
	boolean flag = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		mContext = this;
		setContentView(R.layout.history_ui);
		dataBase = HistoryDataBase.getInstance(this);
		alarmManager = AlarmManager.getInstance(this);
		mData = dataBase.queryItems("24");
		Log.d(" mData.size()", mData.size()+"");
//		initViewValue();
		// cursor = dataBase.queryItems("6");
		listView = (ListView) findViewById(R.id.history_list);
		listView.setVisibility(View.VISIBLE);
		backButton = (ImageButton) findViewById(R.id.back_button);
		backButton.setOnClickListener(new OnClickListener() {
			// @Override
			public void onClick(View arg0) {
				finish();
			}
		});
		adapter = new MyItemAdapter(this);
		// adapter = new MyViewAdapter(this, 300, 200);
		listView.setAdapter(adapter);
		// setContentView(new HistoryView(this, 400, 1000));
		layout = (LinearLayout) findViewById(R.id.history_view);
//		historyButton = (ImageButton) findViewById(R.id.history_button);
//		historyButton.setOnClickListener(new OnClickListener() {
//			@Override
//			public void onClick(View arg0) {
//				if (!flag) {
//					flag=!flag;
//					listView.setVisibility(View.GONE);
//					layout.setVisibility(View.VISIBLE);
//					initViewValue();
//					layout.addView(new HistoryView(mContext, 400, 800));
//				} else {
//					flag=!flag;
//					layout.setVisibility(View.GONE);
//					listView.setVisibility(View.VISIBLE);
//					mData = dataBase.queryItems("24");
//					adapter = new MyItemAdapter(mContext);
//					listView.setAdapter(adapter);
//				}
//
//			}
//		});

	}

	public final class ViewHolder {
		public View mView;
		public TextView PHText;
		public TextView TimeText;
		public TextView TempText;
		public TextView CondText;
		public TextView OxyText;
		public TextView WateText;
	}

	private class MyItemAdapter extends BaseAdapter {
		private LayoutInflater mInflater;

		public MyItemAdapter(Context context) {
			this.mInflater = LayoutInflater.from(context);
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return mData.size();
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder = null;
			if (convertView == null) {
				holder = new ViewHolder();
				holder.mView = mInflater.inflate(R.layout.history_item, null);
				convertView = holder.mView;
				holder.TimeText = (TextView) holder.mView
						.findViewById(R.id.time_hs_value);
				holder.PHText = (TextView) holder.mView
						.findViewById(R.id.ph_hs_value);
				holder.TempText = (TextView) holder.mView
						.findViewById(R.id.temperature_hs_value);
				holder.CondText = (TextView) holder.mView
						.findViewById(R.id.conductivity_hs_value);
				holder.OxyText = (TextView) holder.mView
						.findViewById(R.id.oxygen_hs_value);
				holder.WateText = (TextView) holder.mView
						.findViewById(R.id.waterLevel_hs_value);
				convertView.setTag(holder);

			} else {

				holder = (ViewHolder) convertView.getTag();
			}

			holder.TimeText.setText(mData.get(position).time);
			holder.PHText.setText(mData.get(position).ph + "");
			if (alarmManager.getPhMax() > mData.get(position).ph
					&& alarmManager.getPhMin() < mData.get(position).ph) {
				holder.PHText.setTextColor(Color.GREEN);
			} else {
				holder.PHText.setTextColor(Color.RED);
			}
			holder.TempText.setText(mData.get(position).temperature + "");
			if (alarmManager.getTemperatureMax() > mData.get(position).temperature
					&& alarmManager.getTemperatureMin() < mData.get(position).temperature) {
				holder.TempText.setTextColor(Color.GREEN);
			} else {
				holder.TempText.setTextColor(Color.RED);
			}
			holder.CondText.setText(mData.get(position).conductivity + "");
			if (alarmManager.getConductivityMax() > mData.get(position).conductivity
					&& alarmManager.getConductivityMin() < mData.get(position).conductivity) {
				holder.CondText.setTextColor(Color.GREEN);
			} else {
				holder.CondText.setTextColor(Color.RED);
			}

			holder.OxyText.setText(mData.get(position).oxygen + "");
			if (alarmManager.getOxygenMax() > mData.get(position).oxygen
					&& alarmManager.getOxygenMin() < mData.get(position).oxygen) {
				holder.OxyText.setTextColor(Color.GREEN);
			} else {
				holder.OxyText.setTextColor(Color.RED);
			}
			holder.WateText.setText(mData.get(position).water_level + "");
			if (alarmManager.getWaterLevelMax() > mData.get(position).water_level
					&& alarmManager.getWaterLevelMin() < mData.get(position).water_level) {
				holder.WateText.setTextColor(Color.GREEN);
			} else {
				holder.WateText.setTextColor(Color.RED);
			}
			return convertView;
		}
	}

//	private void initViewValue() {
//		mIconIDs = new Integer[] { R.drawable.ph, R.drawable.temperature,
//				R.drawable.redox, R.drawable.conductivity,
//				R.drawable.water_level };
//		mItemName = new Integer[] { R.string.ph_value,
//				R.string.temperature_value, R.string.oxygen_value,
//				R.string.conductivity_value, R.string.waterLevel_value };
//		mColors = new Integer[] { Color.BLACK, Color.BLUE, Color.RED,
//				Color.GREEN, Color.GRAY };
//		mPaths = new ArrayList<Path>();
//
//		Path mPHpath = new Path();
//		mPHpath.moveTo(100, tmpy * 1 - (mData.get(0).ph * 20));
//		Path mTemppath = new Path();
//		mTemppath.moveTo(100, tmpy * 2 - (mData.get(0).temperature * 2));
//		Path mOxypath = new Path();
//		mOxypath.moveTo(100, tmpy * 3 - mData.get(0).oxygen / 3);
//		Path mCondpath = new Path();
//		mCondpath.moveTo(100, tmpy * 4 - mData.get(0).conductivity * 150);
//		Path mWatepath = new Path();
//		mWatepath.moveTo(100, tmpy * 5 - mData.get(0).water_level / 5);
//		for (int i = 1; i < mData.size(); i++) {
//			mPHpath.lineTo(100 + tmpx * i, tmpy * 1 - (mData.get(i).ph * 20));
//			mTemppath.lineTo(100 + tmpx * i, tmpy * 2
//					- (mData.get(i).temperature * 2));
//			mOxypath.lineTo(100 + tmpx * i, tmpy * 3 - mData.get(i).oxygen / 2);
//			// Log.d("", (100 + tmp * i) + "   "
//			// + (tmpy * 3 - mData.get(i).oxygen / 2));
//			mCondpath.lineTo(100 + tmpx * i, tmpy * 4
//					- mData.get(i).conductivity * 150);
//			// Log.d("", (100 + tmpx * i) + "   "
//			// + (tmpy * 4 - mData.get(i).conductivity * 150));
//			mWatepath.lineTo(100 + tmpx * i, tmpy * 5
//					- mData.get(i).water_level / 5);
//		}
//		mPaths.add(mPHpath);
//		mPaths.add(mTemppath);
//		mPaths.add(mOxypath);
//		mPaths.add(mCondpath);
//		mPaths.add(mWatepath);
//	}

	public class HistoryView extends View {
		private Bitmap mBitmap;
		private Paint mPaint = new Paint();
		private Canvas mCanvas;

		public HistoryView(Context context, int width, int height) {
			super(context);
			mPaint = new Paint();
			mPaint.setStyle(Style.STROKE);
			mBitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
			mCanvas = new Canvas(mBitmap);
//			Bitmap bitmaptmp = BitmapFactory.decodeResource(
//					context.getResources(), R.drawable.background);
//			mCanvas.drawBitmap(bitmaptmp, 0,0, null);
//			bitmaptmp.recycle();
//			mCanvas.drawColor(Color.WHITE);
			for (int i = 0; i < mPaths.size(); i++) {
				mPaint.setColor(mColors[i]);
				mCanvas.drawPath(mPaths.get(i), mPaint);
				Bitmap bitmap = BitmapFactory.decodeResource(
						context.getResources(), mIconIDs[i]);
				mCanvas.drawBitmap(bitmap, 20, i * tmpy + 60, null);
				bitmap.recycle();
			}

		}

		@Override
		protected void onDraw(Canvas canvas) {
			super.onDraw(canvas);
			canvas.drawColor(Color.WHITE);
			canvas.drawBitmap(mBitmap, 0, 0, null);
		}
	}
}
